package bitWork;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: tao
 * Date: 2023-11-14
 * Time: 20:03
 */
public class TestUpdate {
    public static void main(String[] args) throws SQLException {
        //1、建立数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");
        //2、建立连接
        Connection connection = dataSource.getConnection();
        //3、建立一个sql语句
        String sql = "update student set id = 2 where name = '张三'";
        //预处理
        PreparedStatement statement = connection.prepareStatement(sql);
        //4、执行SQL语句
        int n = statement.executeUpdate();
        System.out.println(n);
        //5、回收必要的资源
        statement.close();
        connection.close();
    }
}
